package guiCompent;

import java.util.Vector;

import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.Sprite;

import util.Pair;

public class Menu {
	private Pair pos;
	private int width, height;
	private int bgColor = 0x000000, wordColor = 0xffffff;
	private int left = 0, right = 0, top = 0, bottom = 0;
	private int chose = 0, type = 0, fontSize = 40;
	private Image bgImg = null;
	Vector terms;
	
	public Menu(int x, int y, int w, int h) {
		pos = new Pair(x, y);
		width = w;
		height = h;
		terms = new Vector();
	}
	
	public void setHMode() {type = 0;}
	public void setVMode() {type = 1;}
	public void setBgImg(Image bg) {bgImg = bg;}
	public void setBgColor(int c) {bgColor = c;}
	public void setWordColor(int c) {wordColor = c;}
	public void setLeftMargin(int lm) {left = lm;}
	public void setRightMargin(int rm) {right = rm;}
	public void setTopMargin(int tm) {top = tm;}
	public void setBottomMargin(int bm) {bottom = bm;}
	public void setFontSize(int s) {fontSize = s;}

	public void setChoose(int c) {chose = c;}
	
	public void add(String name) {
		terms.addElement(name);
	}
	public void modify(int index, String name) {
		terms.setElementAt(name, index);
	}
	
	public void paint(Graphics g) {
		if (bgImg != null) {
			g.drawRegion(bgImg, 0, 0, width, height, Sprite.TRANS_NONE,
					pos.first, pos.second, 0);
		}
		else {
			g.setColor(bgColor);
			g.fillRect(pos.first, pos.second, width, height);
		}
		g.setColor(wordColor);
		
		drawInDiffMode(g);
	}
	
	private void drawInDiffMode(Graphics g) {
		//horizonal mode
		if (type == 0) {
			int gridLen = (width - (left + right)) / terms.size();
			for (int i = 0; i < terms.size(); ++i) {
				Font font = Font.getFont(Font.FACE_PROPORTIONAL,
						Font.STYLE_UNDERLINED, fontSize);
				g.setFont(font);
				String str = (String) terms.elementAt(i);
				g.drawString(str, pos.first + left + gridLen * i
						+ (gridLen - font.stringWidth(str)) / 2, pos.second
						+ (height - font.getHeight()) / 2, 0);
			}
			if (chose > -1)
				g.drawRoundRect(pos.first + chose * gridLen + 5,
						pos.second + 5, gridLen - 10, height - 10, 10, 10);
		}
		//vertical mode
		else if (type == 1) {
			int gridHeight = (height - (top + bottom)) / terms.size();
			for (int i = 0; i < terms.size(); ++i) {
				Font font = Font.getFont(Font.FACE_PROPORTIONAL,
						Font.STYLE_UNDERLINED, fontSize);
				g.setFont(font);
				String str = (String) terms.elementAt(i);
				g.drawString(str, pos.first + (width - font.stringWidth(str))/2, 
						pos.second + gridHeight * i
						+ (gridHeight - font.getHeight()) / 2, 0);
			}
			if (chose > -1)
				g.drawRoundRect(pos.first + 5,
						pos.second + chose * gridHeight + 5,
						width - 10, gridHeight - 10, 10, 10);
		}
	}

	public int size() {return terms.size();}
	public int getChose() {return chose;}
}
